package com.yanggu.redisson.controller;

import com.yanggu.redisson.domain.Entry;
import org.redisson.api.RList;
import org.redisson.api.RPriorityBlockingQueue;
import org.redisson.api.RRingBuffer;
import org.redisson.api.RTransferQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.concurrent.TimeUnit;

/**
 * 测试使用Redisson的集合型数据结构
 * <p>Redisson的集合型数据结构大部分实现标准的Java接口，使用起来和Java内置的集合型数据结构类似，api基本上一样</p>
 */
@RestController
@RequestMapping("/test-collection")
public class TestCollectionController {

    @Autowired
    private RList<String> rList;

    @Autowired
    private RPriorityBlockingQueue<Entry<String>> priorityBlockingQueue;

    @Autowired
    @Qualifier("test-ring-buffer")
    private RRingBuffer<Integer> ringBuffer;

    @Autowired
    @Qualifier("test-transfer-queue")
    private RTransferQueue<String> transferQueue;

    //List类型进行读写操作。底层为Redis的List数据类型
    @GetMapping("/test0")
    public void test0() {
        rList.add("1");
        rList.add("2");
        rList.add("3");
        rList.add("4");

        rList.forEach(System.out::println);

        Iterator<String> iterator = rList.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            iterator.remove();
            System.out.println("next = " + next);
        }
    }

    //优先阻塞队列的写操作。使用RLock和Redis的List。使用二分法进行插入到List中
    @GetMapping("/test1")
    public void test1() throws Exception {
        priorityBlockingQueue.put(new Entry<>(1, "1"));
        priorityBlockingQueue.put(new Entry<>(4, "2"));
        priorityBlockingQueue.put(new Entry<>(2, "3"));
        priorityBlockingQueue.put(new Entry<>(3, "4"));
    }

    //优先阻塞队列的读操作
    @GetMapping("/test2")
    public void test2() throws Exception {
        while (!priorityBlockingQueue.isEmpty()) {
            Entry<String> take = priorityBlockingQueue.take();
            //priorityBlockingQueue.take
            System.out.println("take = " + take);
        }
    }

    //有点像有界阻塞队列。队列已满，会驱逐head元素
    // 底层为Redis的List数据类型
    @GetMapping("/test3")
    public void test3() {
        ringBuffer.clear();
        // buffer capacity is 4 elements
        ringBuffer.trySetCapacity(4);

        ringBuffer.add(1);
        ringBuffer.add(2);
        ringBuffer.add(3);
        ringBuffer.add(4);
        // buffer state is 1, 2, 3, 4
        System.out.println(ringBuffer.readAll());

        ringBuffer.add(5);
        ringBuffer.add(6);
        // buffer state is 3, 4, 5, 6
        System.out.println(ringBuffer);
    }

    //transferQueue
    @GetMapping("/test4")
    public void test4() throws Exception {
        transferQueue.transfer("data1");
        // or try transfer immediately
        transferQueue.tryTransfer("data2");
        // or try transfer up to 10 seconds
        transferQueue.tryTransfer("data3", 10, TimeUnit.SECONDS);
    }

    @GetMapping("/test5")
    public void test5() throws Exception {
        while (true) {
            String take = transferQueue.take();
            System.out.println("take = " + take);
        }
    }

}
